package com.hna.eking.AirlineServer.Service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.hna.eking.AirlineServer.Exception.RunServiceException;
import com.hna.eking.AirlineServer.Mapper.AreaInfo;
import com.hna.eking.AirlineServer.Mapper.AreaInfoMapper;

/**
 *
 * 地区Service维护类 Date : 2018-9-13 Description: 地区表Service维护类 Copyright: Copyright
 * &copy; 2018 Company :aivac
 * 
 * @author APP BD lele
 * @version V1.0 修改历史 序号 日期 修改人 修改原因 1
 * 
 */
@Service
public class AreaInfoService {
	@Autowired
	private AreaInfoMapper Mapper;
	private Logger logger = LoggerFactory.getLogger(AirportInfoService.class);

	/*
	 * 查询地区列表
	 * 
	 * 参数： 无
	 * 
	 * 返回： 地区列表
	 *
	 */
	public List<AreaInfo> selectAllByParentCode(String parentCode) throws RunServiceException {
		String subCode = null;
		if (parentCode != null) {
			int size = parentCode.length();
			int i = 0;
			/* 倒序查找最后一位不为0的数的位置 */
			for (i = size - 1; i >= 0; i--) {
				// logger.debug("parentCode:" + parentCode.charAt(i));
				if (parentCode.charAt(i) != '0') {
					break;
				}
			}
			if (i > 0) {
				subCode = parentCode.substring(0, i + 1);
			}

			else {
				subCode = parentCode;
			}
			logger.debug("subCode:" + subCode + "--" + i + "--" + size);
		}
		try {
			List<AreaInfo> AreaList = Mapper.selectAllByAreaCode(subCode);

			/* 没有数据，返回无数据提示 */
			if (AreaList == null || AreaList.size() == 0) {
				throw new RunServiceException(RunServiceException.DATA_NOT_FOUND);
			}
			return AreaList;
		} catch (DataAccessException e) {
			logger.error("DATABASE ERROR");
			e.printStackTrace();
			throw new RunServiceException(RunServiceException.DATABASE_ERROR);
		}

	}

	public List<AreaInfo> selectAll() throws RunServiceException {
		try {
			List<AreaInfo> AreaList = Mapper.selectAll();

			/* 没有数据，返回无数据提示 */
			if (AreaList == null || AreaList.size() == 0) {
				throw new RunServiceException(RunServiceException.DATA_NOT_FOUND);
			}
			return AreaList;
		} catch (DataAccessException e) {
			logger.error("DATABASE ERROR");
			e.printStackTrace();
			throw new RunServiceException(RunServiceException.DATABASE_ERROR);
		}

	}
}
